{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jan => Month.Jan , 1\n",
      "Feb => Month.Feb , 2\n",
      "Mar => Month.Mar , 3\n",
      "Apr => Month.Apr , 4\n",
      "May => Month.May , 5\n",
      "Jun => Month.Jun , 6\n",
      "Jul => Month.Jul , 7\n",
      "Aug => Month.Aug , 8\n",
      "Sep => Month.Sep , 9\n",
      "Oct => Month.Oct , 10\n",
      "Nov => Month.Nov , 11\n",
      "Dec => Month.Dec , 12\n"
     ]
    }
   ],
   "source": [
    "# 枚举类的使用\n",
    "from enum import Enum\n",
    "Month = Enum('Month',('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))\n",
    "for name,member in Month.__members__.items():\n",
    "    print(name,'=>',member,',',member.value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Weekday.Mon\n",
      "True\n",
      "1\n",
      "Weekday.Sat\n"
     ]
    }
   ],
   "source": [
    "from enum import Enum,unique\n",
    "@unique\n",
    "class Weekday(Enum):\n",
    "    Sum = 0\n",
    "    Mon = 1\n",
    "    Tue = 2\n",
    "    Wed = 3\n",
    "    Thu = 4\n",
    "    Fri = 5\n",
    "    Sat = 6\n",
    "day1 = Weekday.Mon\n",
    "print(day1)\n",
    "print(day1 == Weekday.Mon)\n",
    "print(Weekday.Mon.value)\n",
    "print(Weekday['Sat'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试成功\n"
     ]
    }
   ],
   "source": [
    "from enum import Enum,unique\n",
    "class Gender(Enum):\n",
    "    Male = 0\n",
    "    Female = 1\n",
    "class Student(object):\n",
    "    def __init__(self,name,gender):\n",
    "        self.name = name\n",
    "        self.gender = gender\n",
    "bart = Student('bart',Gender.Male)\n",
    "if bart.gender == Gender.Male:\n",
    "    print(\"测试成功\")\n",
    "else:\n",
    "    print(\"测试失败\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class '__main__.Student'>\n",
      "<class 'type'>\n"
     ]
    }
   ],
   "source": [
    "print(type(bart))\n",
    "print(type(Student))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'yaml' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[10], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mclass\u001b[39;00m \u001b[38;5;21;01mMonster\u001b[39;00m(\u001b[43myaml\u001b[49m\u001b[38;5;241m.\u001b[39mYAMLObject):\n\u001b[0;32m      2\u001b[0m     yaml_tag \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mu\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m!Monster\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m      3\u001b[0m     \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\u001b[38;5;28mself\u001b[39m,name,hp,ac,attacks):\n",
      "\u001b[1;31mNameError\u001b[0m: name 'yaml' is not defined"
     ]
    }
   ],
   "source": [
    "class Monster(yaml.YAMLObject):\n",
    "    yaml_tag = u'!Monster'\n",
    "    def __init__(self,name,hp,ac,attacks):\n",
    "        self.name = name\n",
    "        self.hp = hp\n",
    "        self.ac = ac\n",
    "        self.attacks = attacks\n",
    "    def __repr__(self):\n",
    "        return \"%s(name=%r,hp=%r,ac=%r,attacks=%r)\"%(self.__class__.__name__,self.name,self.hp,self.ac,self.attacks)\n",
    "yaml.load(\"\"\"--- !Monstername: Cave spiderhp: [2,6] # 2d6ac: 16attacks: [BITE, HURT]\"\"\")\n",
    "Monster(name='Cave spider', hp=[2, 6], ac=16, attacks=['BITE', 'HURT'])\n",
    "print(yaml.dump(Monster( name='Cave lizard', hp=[3,6], ac=16, attacks=['BITE','HURT'])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "class ListMetaclass(type):\n",
    "    def __new__(cls,name,bases,attrs):\n",
    "        attrs['add'] = lambda self,value:self.append(value)\n",
    "        return type.__new__(cls,name,bases,attrs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class MyList(list,metaclass=ListMetaclass):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'MyList' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[12], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m L \u001b[38;5;241m=\u001b[39m \u001b[43mMyList\u001b[49m()\n\u001b[0;32m      2\u001b[0m L\u001b[38;5;241m.\u001b[39madd(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mHello world\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28mprint\u001b[39m(L)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'MyList' is not defined"
     ]
    }
   ],
   "source": [
    "L = MyList()\n",
    "L.add('Hello world')\n",
    "print(L)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
